package com.jh.user.repository;

import com.jh.user.pojo.Platform;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PlatformRepository extends JpaRepository<Platform,String>,
        JpaSpecificationExecutor<Platform> {

    @Query(value = "SELECT MAX(platform.platformCode)  FROM  Platform platform")
    public String selectPlatform();

    Platform findByPlatformCodeAndState(String platformCode, String state);

    Platform findByPlatformCode(String platformCode);

    @Query(value = "select platForm from Platform platForm where platForm.shopId = :mobilePhone")
    Page<Platform> queryPlatFromByShopId(@Param("mobilePhone") String mobilePhone, Pageable pageable);

    Platform findByShopIdAndState(String shopId, String state);

//    @Query("select pf from Platform pf where pf.agent = :fromAgent and pf.shopId <> null and pf.shopId <> ''")
    @Query("select pf from Platform pf where pf.agent = :fromAgent")
    List<Platform> findByAgent(@Param("fromAgent") String fromAgent);

    @Modifying
    @Query("update Platform pf set pf.agent = :toAgent where pf.platformId = :platformId")
    int updatePlatform(@Param("toAgent") String toAgent, @Param("platformId") String platformId);

    @Query("select p from Platform p where p.agent = :agent")
    List<Platform> getPlatform(@Param("agent") String agent);
}
